Page({
  data: {
    currSong: [],
    lyric:[],
    isPlay:true,
  },
  onShow: function (e) {
    
    var that = this;
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio');
    // console.log(this.audioCtx);

    // console.log(getApp().globalData.songmsg);
    // 获取list当前点击歌曲的信息
    this.setData({
      currSong: getApp().globalData.songmsg
    })
    this.audioCtx.play();

    // 获取音频总长度
    wx.getBackgroundAudioPlayerState({
      success: function (res) {
        console.log(12344)
        var status = res.status;
        var dataUrl = res.dataUrl;
        var currentPosition = res.currentPosition;
        var duration = res.duration;
        var downloadPercent = res.downloadPercent;
        console.log("status" + status);
        console.log("dataUrl" + dataUrl);
        console.log("currentPosition" + currentPosition);
        console.log("duration" + duration);
        console.log("downloadPercent" + downloadPercent);
      }
    })
   
    // 获取歌词
    var geciId = getApp().globalData.songmsg.songid;
    var lyricObj = [];
    wx.request({
      url: "https://route.showapi.com/213-2",
      data: {
        showapi_appid: 41290,
        showapi_sign: "d395a1c332314f8587ce9db0ebaf751c",
        musicid: geciId
      },
      method: "GET",
      success: function (res) {
        // console.log(res);
        var datas = res.data;
        if (datas.showapi_res_code == 0) {
          var lyric = datas.showapi_res_body.lyric;
          // console.log(lyric);
          // 将里面ascii 码转换 ,并且根据  \n 分割为 数组
          var lyricArr = lyric.replace(/&#\d+;/g, function (char) {
            return String.fromCharCode(char.substr(2, 2))
          }).split('\n');

          // 对 数组做循环，做一系列处理
          for (var i = 0; i < lyricArr.length; i++) {
            var time_text = lyricArr[i].split(']');
            var text = time_text[1];	// 歌词文本

            // console.log(text);
            // 把没有歌词给过滤掉
            if (!text) {
              continue;
            }

            // 对 时间  time_text[0]  [02:18.00  转换成 秒数
            var time = time_text[0].substr(1).split(':');
            time = time[0] * 60 + parseInt(time[1]);
            // console.log(time);

            var obj = {
              text: text,
              time: time
            }

            lyricObj.push(obj);
          }

          // console.log(lyricObj);
          that.setData({
            lyric: lyricObj
          })

        }
      }
    })
    
  },
  audioPlay: function () {
    var currPlay = this.data.isPlay;
    if(currPlay==true){
      this.audioCtx.pause()
    }else{
      this.audioCtx.play();
    }
    this.setData({
      isPlay: !this.data.isPlay
    })
  },
  audioPause: function () {
    
  }
})


// wx.pageScrollTo({
//   scrollTop: 0
// })